#define _CRT_SECURE_NO_WARNINGS 1


int BTSize(struct TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }
    return BTSize(root->left) + BTSize(root->right) + 1;
}

void preorder(struct TreeNode* root, int* a, int* pi)
{
    if (root == NULL)
        return;
    a[(*pi)++] = root->val;

    preorder(root->left, a, pi);
    preorder(root->right, a, pi);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
    int n = BTSize(root);
    int* a = (int*)malloc(sizeof(int) * n);
    *returnSize = n;

    int i = 0;
    preorder(root, a, &i);
    return a;
}